<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

class table_position extends discuz_table
{
    public function __construct() {
        $this->_table = 'er_gridad_combo_position';
        $this->_pk = 'id';
        parent::__construct();
    }

    public function fetch($condition = array(), $order = 'sort ASC, id ASC') {
        $where = '1';
        $params = array($this->_table);
        
        if(!empty($condition)) {
            foreach($condition as $field => $value) {
                if(is_array($value)) {
                    $where .= " AND $field IN(%n)";
                    $params[] = $value;
                } else {
                    $where .= " AND $field=%d";
                    $params[] = $value;
                }
            }
        }
        
        return DB::fetch_all("SELECT * FROM %t WHERE $where ORDER BY $order", $params);
    }

    // 获取单个广告位
    public function fetch_one($condition) {
        $result = $this->fetch($condition, '', 1);
        return $result ? $result[0] : false;
    }

    // 更新广告位排序
    public function update_sort($id, $sort) {
        return DB::query('UPDATE %t SET sort=%d WHERE id=%d', array($this->_table, $sort, $id));
    }

    // 检查广告位名称是否已存在
    public function check_name_exists($name, $exclude_id = 0) {
        $sql = $exclude_id ? 'AND id!=%d' : '';
        $params = $exclude_id ? array($this->_table, $name, $exclude_id) : array($this->_table, $name);
        return DB::fetch_first('SELECT id FROM %t WHERE name=%s '.$sql, $params);
    }
}